﻿using CleanArchitecture.Infrastructure.Entities;
using CleanArchitecture.Infrastructure.Entities.Settings;
using CleanArchitecture.Infrastructure.Interfaces;
using CleanArchitecture.Infrastructure.Interfaces.Security;
using CleanArchitecture.Infrastructure.Interfaces.SystemManage;
using CleanArchitecture.Infrastructure.Models.AdminUsers;
using CleanArchitecture.Web.Factories;
using CleanArchitecture.Web.Filters;
using Microsoft.AspNetCore.Mvc;

namespace CleanArchitecture.Web.Controllers.Backend
{
    public class BackendAdminUserController : BaseAdminController
    {
        private readonly IAdminUserModelFactory _adminusermodelFactory;
        private readonly AdminUserSettings _adminuserSettings;
        private readonly IWorkContext _workContext;
        private readonly IPermissionService _permissionService;
        private readonly ISystemModuleService _systemmoduleService;
        public BackendAdminUserController(IAdminUserModelFactory adminusermodelFactory,
            AdminUserSettings adminuserSettings,
            IWorkContext workContext,
            IPermissionService permissionService,
            ISystemModuleService systemmoduleService)
        {
            this._adminusermodelFactory = adminusermodelFactory;
            this._adminuserSettings = adminuserSettings;
            this._workContext = workContext;
            this._permissionService = permissionService;
            this._systemmoduleService = systemmoduleService;
        }
       
        //[AuthorizeAdmin(Core.Configuration.AuthorizeType.AdminOnly)]
        public IActionResult AccountSetting()
        {
            var model = _adminusermodelFactory.PrepareAdminUser();
            ViewBag.PageTitle = "个人设置 >_<";
            return View(model);
        }

        public IActionResult AccountChangePassword()
        {
            var model = new AdminUserModel();
            model.Id = _workContext.CurrentAdminUser.Id;
            ViewBag.PageTitle = "更新账户密码";
            ViewBag.PasswordMinLen = _adminuserSettings.PasswordMinLength;
            ViewBag.PasswordMaxLen = _adminuserSettings.PasswordMaxLength;
            return View(model);
        }
        /// <summary>
        /// 系统管理-用户管理
        /// </summary>
        /// <returns></returns>
        //[Route(BackendAuthorizeRouteConfig.BackendAdminUser_AdminUserManage)]
        public virtual IActionResult AdminUserManage()
        {
            string moduleUri = BackendAuthorizeRouteConfig.BackendAdminUser_AdminUserManage;
            var CurrentSystemModule = _systemmoduleService.GetSystemModuleByUri(moduleUri);
            if (!_permissionService.Authorize(CurrentSystemModule))
                return AccessDeniedView();
            ViewBag.PageTitle = "系统用户管理";
            ViewBag.CurrentSystemModuleId = CurrentSystemModule.Id;
            var searchModel = new AdminUserSearchModel();
            searchModel.SetGridPageSize();
            searchModel.CustomProperties.Add("adminuserModel", new AdminUserModel());
            return View(searchModel);
        }
        [AuthorizeButton]
        public virtual IActionResult AdminUserManageEdit(int moduleid, int buttonid, int userid)
        {
            ViewBag.PageTitle = "编辑用户信息";
            var userModel = userid == 0
                          ? _adminusermodelFactory.PrepareNewRegistedAdminUser()
                          : _adminusermodelFactory.PrepareAdminUser(userid);
            
            ViewBag.PasswordMinLen = _adminuserSettings.PasswordMinLength;
            ViewBag.PasswordMaxLen = _adminuserSettings.PasswordMaxLength;
            return View(userModel);
        }
        /// <summary>
        /// 系统管理-用户角色管理
        /// </summary>
        /// <returns></returns>
        public virtual IActionResult AdminRoleManage()
        {
            string moduleUri = BackendAuthorizeRouteConfig.BackendAdminUser_AdminRoleManage;
            var CurrentSystemModule = _systemmoduleService.GetSystemModuleByUri(moduleUri);
            if (!_permissionService.Authorize(CurrentSystemModule))
                return AccessDeniedView();
            ViewBag.PageTitle = "系统角色管理";
            ViewBag.CurrentSystemModuleId = CurrentSystemModule.Id;
            var searchModel = new AdminRoleSearchModel();
            searchModel.SetGridPageSize();
            searchModel.CustomProperties.Add("adminroleModel", new AdminRoleModel());
            return View(searchModel);
        }
        [AuthorizeButton]
        public virtual IActionResult AdminRoleManageEdit(int moduleid, int buttonid, int roleid)
        {
            ViewBag.PageTitle = "编辑用户角色信息";
            var roleModel = roleid == 0
                          ? new AdminRoleModel()
                          : _adminusermodelFactory.PrepareAdminRoleModel(roleid);
            return View(roleModel);
        }
    }
}